package co.yixiang.modules.shop.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import co.yixiang.modules.shop.entity.YxSalesManage;
import co.yixiang.modules.shop.web.param.YxSalesManageQueryParam;
import co.yixiang.modules.shop.web.vo.YxSalesManageQueryVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 主管业务员表 Mapper 接口
 * </p>
 *
 * @author hisen
 * @since 2020-07-31
 */
@Repository
public interface YxSalesManageMapper extends BaseMapper<YxSalesManage> {

    /**
     * 根据ID获取查询对象
     * @param id
     * @return
     */
    YxSalesManageQueryVo getYxSalesManageById(Serializable id);

    /**
     * 获取分页对象
     * @param page
     * @param yxSalesManageQueryParam
     * @return
     */
    IPage<YxSalesManageQueryVo> getYxSalesManagePageList(@Param("page") Page page, @Param("param") YxSalesManageQueryParam yxSalesManageQueryParam);

    /**
     * 查询业务员列表
     * @param managerId
     * @return
     */
    @Select("select sale.id,sale.uid, manageUid, saleName,IFNULL(orderCount,0) orderCount,IFNULL(commission,0)  commission from (\n" +
            "\n" +
            " select manage.id,manage.manage_uid manageUid, sale.real_name saleName,  sale.uid   from yx_sales_manage  manage ,yx_sales sale where manage.uid = sale.uid and manage.manage_uid = #{managerId})\n" +
            "sale left join\n" +
            "(select sales_man_id,count(1) orderCount,sum(commission) commission from yx_sales_commission_detail group by sales_man_id) invest_order\n" +
            "on sale.uid = invest_order.sales_man_id\n" +
            "where sale.manageUid = #{managerId}\n" +
            "  ")
    List<YxSalesManageQueryVo> getListByManagerId(int managerId);



    /**
     * 查询所有业务员列表
     * @return
     */
    @Select(" select  sale.uid, 0 as manageUid,phone, sale.real_name as  saleName,IFNULL(orderCount,0) orderCount,IFNULL(commission,0) commission  from yx_sales sale\n" +
            " left join (select sales_man_id,count(1) orderCount,sum(commission) commission\n" +
            " from yx_sales_commission_detail group by sales_man_id) invest_order on sale.uid = invest_order.sales_man_id where sale.group_id = '1' ")
    List<YxSalesManageQueryVo> getAllSales();

}
